package com.tyjc.unique.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.tyjc.unique.util.AppUtil;
import com.tyjc.lottery.R;


public class RandomSetView extends LinearLayout {
	//最小个数
	private int minRandomNum = 0;
	//最大个数
	private int maxRandomNum = 0;
	//标题
	private String title;

	private TextView lineTitle;
	private TextView tag_line;
	private Button countBtn;
	private Button jxaction;

	private Context context;

	private int choseNum = minRandomNum;

	public RandomSetView(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		initView(context, attrs);
	}

	public RandomSetView(Context context, AttributeSet attrs) {
		super(context, attrs);
		initView(context, attrs);
	}

	public RandomSetView(Context context) {
		super(context);
		initView(context, null);
	}

	private void initView(Context context, AttributeSet attrs) {
		this.context = context;
		View.inflate(context, R.layout.layout_random_num, this);
		if (isInEditMode()) {
			return;
		}
		if (null != attrs) {
			TypedArray type = context.obtainStyledAttributes(attrs, R.styleable.RandomSetView);
			title = type.getString(R.styleable.RandomSetView_lineTitle);
			minRandomNum = type.getInt(R.styleable.RandomSetView_minJxNm, 0);
			maxRandomNum = type.getInt(R.styleable.RandomSetView_maxJxNm, 0);
			type.recycle();
		}
		choseNum = minRandomNum;
		tag_line = (TextView)findViewById(R.id.tag_line);
		lineTitle = (TextView) findViewById(R.id.random_num_title);
		countBtn = (Button) findViewById(R.id.random_num_count);
		jxaction = (Button) findViewById(R.id.random_num_action);
		lineTitle.setText(title);
		countBtn.setText(String.valueOf(minRandomNum));
		jxaction.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (listener != null) {
					listener.onRandomBtnClick(RandomSetView.this, choseNum);
				}
			}
		});
		countBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				createDialog();
			}
		});
	}

	public void setDefaultChoseNum(int defalutchoseNum) {
		this.choseNum = defalutchoseNum;
		countBtn.setText(String.valueOf(defalutchoseNum));
	}


	public void setRandomBtnVisible(int visible) {
		countBtn.setVisibility(visible);
		jxaction.setVisibility(visible);
	}

	public void setRandomNumRange(int minNum, int maxNum) {
		minRandomNum = minNum;
		maxRandomNum = maxNum;
		choseNum = minRandomNum;
		countBtn.setText(String.valueOf(minRandomNum));
	}

	public void setTag_line(String text,int res) {
		tag_line.setText(text);
		tag_line.setBackgroundResource(res);
	}

	public void setLineTitle(int resId) {
		lineTitle.setText(resId);
	}

	public void setLineTitle(String resId) {
		lineTitle.setText(resId);
	}


	private PopupWindow popupwindow;

	private void createDialog() {
		LayoutInflater inflate = (LayoutInflater) context.getSystemService(Context
				.LAYOUT_INFLATER_SERVICE);
		View popupView = inflate.inflate(R.layout.buy_jx_btn_window, this, false);
		LinearLayout layout1 = (LinearLayout) popupView.findViewById(R.id.buy_jx_btn_layout1);
		LinearLayout layout2 = (LinearLayout) popupView.findViewById(R.id.buy_jx_btn_layout2);
		layout1.getBackground().setAlpha(200);
		layout2.getBackground().setAlpha(200);
		popupwindow = new PopupWindow(popupView, LayoutParams.MATCH_PARENT, LayoutParams
				.WRAP_CONTENT);
		popupwindow.setTouchable(true); // 设置PopupWindow可触摸
		popupwindow.setOutsideTouchable(true);
		popupView.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				if (popupwindow != null && popupwindow.isShowing()) {
					popupwindow.dismiss();
					popupwindow = null;
				}

				return false;
			}
		});
		initBtnWidth();
		addBtn(layout2, minRandomNum, maxRandomNum);
		popupwindow.showAsDropDown(countBtn);
	}

	private final int LINE_NUM = 8;

	private void addBtn(LinearLayout layoutView, int startNum, int sizeNum) {
		int allNum = sizeNum - (startNum - 1);
		int line = allNum / LINE_NUM;
		int lastLineNum = allNum % LINE_NUM;
		if(lastLineNum!=0){
			line=line+1;
		}
		int lineStart = startNum - 1;
		for (int i = 0; i < line; i++) {
			if (i == line - 1 && lastLineNum == 0) {
				lineStart = initLine(layoutView, lineStart, LINE_NUM, false);
			} else {
				lineStart = initLine(layoutView, lineStart, LINE_NUM, true);
			}
		}
	}

	private int width, padding;


	private void initBtnWidth() {
		int widthAll = AppUtil.getWidth();
		width = AppUtil.dip2px(context, 35f);
		padding = (widthAll - width * LINE_NUM) / (LINE_NUM + 1);
	}

	public interface RandomBtnClickListener {
		void onRandomBtnClick(RandomSetView v, int randomCount);
	}

	RandomBtnClickListener listener;

	public void setOnRandomBtnClickListener(RandomBtnClickListener listener) {
		this.listener = listener;
	}


	private int initLine(LinearLayout layoutView, int startNum, int lineNum, boolean isLast) {
		LinearLayout layout = new LinearLayout(context);
		layout.setWeightSum(LINE_NUM);
		for (int j = 0; j < lineNum; j++) {
			final Button btn = new Button(context);
			startNum++;
			btn.setText(startNum + "");
			LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(0 , AppUtil.dip2px
					(context, 40));
			param.setMargins(8, 8, 0, 0);
			param.weight = 1;
			btn.setTextSize(16);
			btn.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					choseNum = Integer.parseInt(btn.getText().toString());
					if (listener != null) {
						listener.onRandomBtnClick(RandomSetView.this, choseNum);
					}
					countBtn.setText(String.valueOf(choseNum));
					popupwindow.dismiss();
				}
			});
			layout.addView(btn, param);
			if(startNum == maxRandomNum){
				break;
			}
		}
		if (isLast) {
			layoutView.addView(layout);
		} else {
			LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
			param.setMargins(0, 0, 0, padding);
			layoutView.addView(layout, param);
		}
		return startNum;
	}

}
